package com.joker.userExam.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.joker.userExam.dao.mapper.SubjectOneQuestionMapper;
import com.joker.userExam.dao.repo.SubjectOneQuestionRepo;
import com.joker.userExam.model.SubjectOneQuestion;
import com.joker.userExam.service.ISubjectOneQuestionManage;
import com.joker.userExam.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author 李杰
 * @date 2021/5/6 - 21:45
 */
@Service
public class SubjectOneQuestionManageImpl implements ISubjectOneQuestionManage {
    @Autowired
    SubjectOneQuestionRepo subjectOneQuestionRepo;
    @Autowired
    SubjectOneQuestionMapper subjectOneQuestionMapper;

    @Override
    public ResultUtil saveSubjectOneQuestion(SubjectOneQuestion oneQuestion) {
        boolean save = subjectOneQuestionRepo.save(oneQuestion);

        if (save) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

    @Override
    public ResultUtil deleteSubjectOneQuestion(Integer id) {
        int i = subjectOneQuestionMapper.deleteById(id);
        if (i == 1) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

    @Override
    public ResultUtil updateSubjectOneQuestion(SubjectOneQuestion oneQuestion) {
        boolean res = subjectOneQuestionRepo.updateById(oneQuestion);
        if (res) {
            return ResultUtil.ok("success");
        } else {
            return ResultUtil.error("error");
        }
    }

    @Override
    public ResultUtil queryAllSubjectOneQuestion() {
        List<SubjectOneQuestion> list = subjectOneQuestionRepo.list();
        return ResultUtil.ok("success", list);
    }

    @Override
    public ResultUtil querySubjectOneQuestionByCondition(SubjectOneQuestion oneQuestion) {
        QueryWrapper<SubjectOneQuestion> queryWrapper = new QueryWrapper<>();
        if (oneQuestion.getType() != null) {
            queryWrapper.eq("type", oneQuestion.getType());
        }
        if(!oneQuestion.getQuestion().isEmpty()){
            queryWrapper.like("question",oneQuestion.getQuestion());
        }

        List<SubjectOneQuestion> list = subjectOneQuestionRepo.list(queryWrapper);


        return ResultUtil.ok("success",list);
    }
}
